﻿using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace PlusTween
{
    public class PlusTweenPosition : PlusTweenBase
    {
        public Vector2 from = Vector3.zero;
        public Vector2 to = Vector3.zero;


        RectTransform mTrans;
        public RectTransform cachedTransform { get { if (mTrans == null) { mTrans = transform.GetComponent<RectTransform>(); } return mTrans; } }

       

        public Vector2 value
        {
            set
            {
               

                cachedTransform.anchoredPosition = value;
            }
            get
            {
                return cachedTransform.anchoredPosition;
            }
        }

        public TweenRectPosition tween
        {
            get
            {
                if (null == _tween) { _tween = this.gameObject.GetComponent<TweenRectPosition>(); }
                if (null == _tween) {
                    _tween = this.gameObject.AddComponent<TweenRectPosition>();
                    _tween.animationCurve = animationCurve;
                    tween.enabled = false;
                    tween.useFixedUpdate = true;
                }

                
                return _tween as TweenRectPosition;
            }
        }

        private void OnEnable()
        {
            if (isBronOrigin) { SetValueFrom(); }
        }
        private void OnDisable()
        {
            tween.enabled = false;
        }


        public override void StartIn()
        {
            tween.duration = duration;
            tween.from = from;
            tween.to = to;

            if (AnimationOrTween.Direction.Forward == tween.direction)
            {
                tween.ResetToBeginning();
                tween.PlayForward();
            }
            else if (AnimationOrTween.Direction.Reverse == tween.direction)
            {
                tween.tweenFactor = 0;
                tween.PlayForward();
            }
        }

        public override void StartOut()
        {
            tween.duration = duration;
            tween.from = from;
            tween.to = to;

            if (AnimationOrTween.Direction.Forward == tween.direction)
            {
                tween.tweenFactor = 1;
                tween.PlayReverse();
            }
            else if (AnimationOrTween.Direction.Reverse == tween.direction)
            {
                tween.ResetToBeginning();
                tween.PlayReverse();
            }
        }

        public override void FinishIn()
        {
            base.FinishIn();
            SetValueTo();
        }


        public override void SetValueFrom() { value = from;  }
        public override void SetValueTo() { value = to; }

    }
}
